<template>
  <dialog-box
    :isDialog="false"
    ref="myDialog"
    :showFooter="true"
    width="1000px"
    @close="close"
    @sureHandle="sureHandle"
    :isFooterSlot="true"
    :title="state.title"
  >
    <template v-slot:dialogMain>
      <div>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="auto"
          class="demo-ruleForm"
          size="default"
          status-icon
        >
          <el-form-item label="取数任务名称" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
          <el-form-item label="任务描述" prop="dscr">
            <el-input v-model="ruleForm.dscr" type="textarea" />
          </el-form-item>
          <el-form-item label="取数类型" prop="type">
            <el-select v-model="ruleForm.type">
              <el-option
                v-for="(it, i) in state.fetchTypes"
                :key="i"
                :label="it.key"
                :value="it.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="周期" prop="zq" v-if="ruleForm.type == '2'">
            <el-select v-model="ruleForm.zq">
              <el-option
                v-for="(it, i) in state.cycleTypes"
                :key="i"
                :label="it.key"
                :value="it.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="时间" prop="time" v-if="ruleForm.type == '2'">
            <el-time-picker
              v-model="ruleForm.time"
              format="hh:mm:ss"
              value-format="hh:mm:ss"
            />
            <span class="ml-10">每月、周、日第一天的几点才能取数</span>
          </el-form-item>
          <el-form-item label="取数配置选择" prop="qspzxz">
            <el-select v-model="ruleForm.qspzxz">
              <el-option
                v-for="(it, i) in state.configTypes"
                :key="i"
                :label="it.key"
                :value="it.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="数据有效期" prop="sjyxq">
            <el-select v-model="ruleForm.sjyxq">
              <el-option
                v-for="(it, i) in state.dataTypes"
                :key="i"
                :label="it.key"
                :value="it.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </template>
  </dialog-box>
</template>

<script setup lang="ts">
import dialogBox from "@/components/dialog-box/index.vue";
import { onMounted, computed, reactive, toRefs, ref } from "vue";
import type { ElMessage, FormInstance, FormRules } from "element-plus";
import apiAxios from "@/api/SelfFetch/fetch-task-manage";
import { PAth } from "@/api/SelfFetch/query";
const ruleForm = ref(PAth());
const myDialog = ref(null);
const isEdit = ref<boolean>(false)
const ruleFormRef = ref<FormInstance>();

let state = reactive({
  title: "",
  fetchTypes: [
    { key: "立即取数", value: '0' },
    { key: "预约取数", value: '1' },
    { key: "周期取数", value: '2' },
  ],
  cycleTypes: [
    { key: "每月", value: '1' },
    { key: "每周", value: '2' },
    { key: "每日", value: '3' },
  ],
  configTypes: [
    { key: "xx", value: '1' },
    { key: "xxx", value: '2 '},
    { key: "xxxx", value: '3' },
  ],
  dataTypes: [
    { key: "1个月", value: '1' },
    { key: "3个月", value: '2' },
    { key: "6个月", value: '3' },
  ],
});
// 定义完成后，用defineExpose()暴露给父组件
const open = (title: string, data: any) => {
  state.title = title;
  if(data){
    isEdit.value = true;
    ruleForm.value = data;
  }
  myDialog.value.handleOpen();
};
const close = () => {
  resetForm();
  myDialog.value.handleClose();
};
const sureHandle = () => {
  submitForm();
};
defineExpose({
  open,
  close,
});

const emit = defineEmits(['successHand'])
const submitForm = async () => {
  if (!ruleFormRef) return;
  console.log(ruleFormRef, "23232");
  await ruleFormRef.value?.validate((valid, fields) => {
    if (valid) {
      console.log("submit!",ruleForm.value);
      setSubData()
    } else {
      console.log("error submit!", fields);
    }
  });
};
const setSubData = async () => {
  console.log("提交数据", ruleForm.value);
  let obj = { ...ruleForm.value };
  let key = isEdit.value ? 'editData' : 'addData';
  let keyValue = isEdit.value ? '编辑' : '新增';

  let res = await apiAxios[key](obj);
  if (res) {
    ElMessage({
      type: "success",
      message: keyValue + "成功!",
    });
    emit('successHand')
    close()
  }
};
const resetForm = () => {
  if (!ruleFormRef) return;
  ruleFormRef.value?.resetFields();
};
const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: "请输入取数任务名称", trigger: "blur" },
  ],
});
</script>

<style scoped>
h2 {
  margin-bottom: 20px;
}
.mb-20 {
  margin-bottom: 20px;
}
.mr-10 {
  margin-right: 10px;
}
.ml-10 {
  margin-left: 10px;
}
.steps-box {
  width: 300px;
  margin: 0 auto 30px;
}
.title-box {
  display: flex;
  align-items: center;
}
.seach-box {
  display: flex;
  padding: 0 10px;
  box-sizing: border-box;
}
.footer-btns {
  width: 100%;
  display: flex;
  justify-content: center;
}
</style>
